const EnhancedOPCrawler = require('../src/enhancedOPCrawler');
const fs = require('fs-extra');

/**
 * 百度贴吧楼主发言爬取示例
 * @description 展示如何使用增强版OP爬取器获取完整的楼主发言
 */
async function basicUsageExample() {
  console.log('🚀 百度贴吧楼主发言爬取示例');
  
  try {
    // 1. 读取cookies（可选，但推荐）
    let cookies = '';
    if (await fs.pathExists('./cookies.txt')) {
      cookies = await fs.readFile('./cookies.txt', 'utf8');
      console.log('✅ 已加载cookies文件');
    }

    // 2. 创建增强版爬取器
    const crawler = new EnhancedOPCrawler({
      maxPages: 100,        // 最大爬取页数
      delay: 3000,          // 基础延迟时间(毫秒)
      cookies: cookies.trim()
    });

    // 3. 设置目标帖子URL
    const postUrl = 'https://tieba.baidu.com/p/5868510485'; // 示例帖子
    
    console.log(`🎯 开始爬取: ${postUrl}`);
    
    // 4. 开始爬取
    const startTime = Date.now();
    const result = await crawler.crawlCompleteOP(postUrl, 'example_output');
    const duration = Math.round((Date.now() - startTime) / 1000);

    // 5. 输出结果
    console.log('\n🎉 爬取完成！');
    console.log(`📊 爬取结果:`);
    console.log(`   ├─ 帖子标题: ${result.postInfo.title}`);
    console.log(`   ├─ 楼主昵称: ${result.postInfo.opName}`);
    console.log(`   ├─ 楼主发言: ${result.totalPosts}楼`);
    console.log(`   ├─ 总字数: ${result.totalWords}字`);
    console.log(`   ├─ 平均字数: ${Math.round(result.totalWords / result.totalPosts)}字/楼`);
    console.log(`   ├─ 保存文件: ${result.savedFiles.length}个`);
    console.log(`   └─ 耗时: ${Math.round(duration/60)}分${duration%60}秒`);
    
    console.log('\n💾 已保存文件:');
    result.savedFiles.forEach(file => {
      console.log(`   📄 ${file}`);
    });

  } catch (error) {
    console.error('\n❌ 爬取失败:', error.message);
    
    if (error.message.includes('403')) {
      console.log('\n💡 建议解决方案:');
      console.log('   1. 确保cookies.txt文件存在且有效');
      console.log('   2. 增加延迟时间参数');
      console.log('   3. 检查网络连接');
    }
  }
}

/**
 * 批量爬取示例
 */
async function batchCrawlExample() {
  console.log('\n🔄 批量爬取示例');
  
  const postUrls = [
    'https://tieba.baidu.com/p/5868510485',
    // 'https://tieba.baidu.com/p/另一个帖子ID',
    // 添加更多帖子URL...
  ];
  
  try {
    let cookies = '';
    if (await fs.pathExists('./cookies.txt')) {
      cookies = await fs.readFile('./cookies.txt', 'utf8');
    }

    const crawler = new EnhancedOPCrawler({
      maxPages: 50,
      delay: 5000,  // 批量爬取时增加延迟
      cookies: cookies.trim()
    });

    for (let i = 0; i < postUrls.length; i++) {
      const url = postUrls[i];
      console.log(`\n📖 爬取第${i + 1}/${postUrls.length}个帖子: ${url}`);
      
      try {
        const result = await crawler.crawlCompleteOP(url, `batch_${i + 1}`);
        console.log(`   ✅ 完成: ${result.totalPosts}楼, ${result.totalWords}字`);
        
        // 批量爬取间隔
        if (i < postUrls.length - 1) {
          console.log('   ⏸️  等待间隔...');
          await new Promise(resolve => setTimeout(resolve, 10000));
        }
        
      } catch (error) {
        console.error(`   ❌ 第${i + 1}个帖子失败: ${error.message}`);
      }
    }

  } catch (error) {
    console.error('❌ 批量爬取失败:', error.message);
  }
}

// 根据命令行参数选择示例
if (require.main === module) {
  const args = process.argv.slice(2);
  
  if (args.includes('--batch')) {
    batchCrawlExample().catch(console.error);
  } else {
    basicUsageExample().catch(console.error);
  }
}

module.exports = {
  basicUsageExample,
  batchCrawlExample
}; 